package com.pyue.eam.regi.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyue.eam.admin.api.entity.regi.CqRegiLibEntity;
import com.pyue.eam.admin.api.entity.regi.CqTaskLibEntity;
import com.pyue.eam.admin.api.entity.regi.CqTaskPlanEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.admin.utils.WxPushMessageUtil;
import com.pyue.eam.app.service.AppUserService;
import com.pyue.eam.common.data.resolver.ParamResolver;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.mapper.CqTaskLibMapper;
import com.pyue.eam.regi.mapper.CqTaskPlanMapper;
import com.pyue.eam.regi.mapper.PartLibMapper;
import com.pyue.eam.regi.service.CqRegiLibService;
import com.pyue.eam.regi.service.CqTaskPlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;

/**
 * 任务方案
 *
 * @date 2023-12-09 20:06:19
 */
@Service
@AllArgsConstructor
public class CqTaskPlanServiceImpl extends ServiceImpl<CqTaskPlanMapper, CqTaskPlanEntity> implements CqTaskPlanService {
    private final CqRegiLibService cqRegiLibService;
    private final CqTaskLibMapper cqTaskLibMapper;
    private final PartLibMapper partLibMapper;
    private final AppUserService appUserService;

    @Override
    public Long saveTaskPlan(CqTaskPlanEntity cqTaskPlan) {
        ApiAsserts.isTrue(cqTaskPlan.getTaskId() != null, "任务id不能为空");
        CqTaskLibEntity cqTaskLib = cqTaskLibMapper.selectById(cqTaskPlan.getTaskId());
        ApiAsserts.isTrue(cqTaskLib != null, "任务不存在");

        assert cqTaskLib != null;
        ApiAsserts.isTrue(Convert.toInt(cqTaskPlan.getCountPrice(), 0) > 0, "任务方案价格不能为空且大于0");

        // 计时情况保存工时费
        if ("1".equals(cqTaskPlan.getTactics())) {
            ApiAsserts.isTrue(cqTaskPlan.getWorkHour() != null && Convert.toDouble(cqTaskPlan.getWorkHour(), 0D) > 0,
                    "任务方案缺少工时费");
        }

        // 团名称 & 团头像
        cqTaskPlan.setChargeAvatar(SecurityUtils.getUserAvatar());
        CqRegiLibEntity regiLibEntity = cqRegiLibService.getRegiLibByUserId(SecurityUtils.getUserId());
        ApiAsserts.nonEmpty(regiLibEntity, "请先完成团长信息填写");

        cqTaskPlan.setRegiName(regiLibEntity.getRegiName());
        cqTaskPlan.setRegiId(regiLibEntity.getRegiId());

        boolean save = save(cqTaskPlan);

        // 上传方案通知企业方
        saveTaskPlanNotice(save, cqTaskLib.getPartId(), cqTaskLib.getCreateBy(), regiLibEntity.getRegiName(), cqTaskPlan.getPerNum());

        return cqTaskPlan.getId();
    }

    @Override
    public Boolean updateDataById(CqTaskPlanEntity cqTaskPlan) {
        ApiAsserts.isTrue(cqTaskPlan.getTaskId() != null, "任务id不能为空");
        CqTaskLibEntity cqTaskLibEntity = cqTaskLibMapper.selectById(cqTaskPlan.getTaskId());
        ApiAsserts.isTrue(cqTaskLibEntity != null, "任务不存在");

        // 计时情况保存工时费
        if ("1".equals(cqTaskPlan.getTactics())) {
            ApiAsserts.isTrue(cqTaskPlan.getWorkHour() != null && Convert.toDouble(cqTaskPlan.getWorkHour(), 0D) > 0,
                    "任务方案缺少工时费");
        }
        return updateById(cqTaskPlan);
    }

    private void saveTaskPlanNotice(boolean save, String partId, String createBy, String regiName, Integer perNum) {
        String wxOpenId = appUserService.getWxOpenIdByAppUserId(createBy);
        if (save && StrUtil.isNotBlank(wxOpenId) && ParamResolver.getBool("MP_NOTICE")) {
            // 发送微信公众号通知-企业
            HashMap<String, String> dataMap = MapUtil.newHashMap();
            dataMap.put("thing2", partLibMapper.getNameById(partId) + "-方案上报");  // 产品名称
            dataMap.put("thing5", regiName + "-团长");  // 报工人员-团名称
            dataMap.put("thing3", perNum + "人");  // 报工数量
            dataMap.put("time4", LocalDate.now().toString());  // 报工时间
            // 推送消息
            WxPushMessageUtil.templateMessage(wxOpenId, "doTZqROv-7pqr_micBOJuYoD3f_B5SM7MUu-6sE2LJU", dataMap);
        }
    }

    /**
     * 计算任务方案费用
     */
    // private static Double countTaskPlanPrice(CqTaskPlanEntity cqTaskPlan) {
    //     CqTaskLibService taskLibService = SpringContextHolder.getBean(CqTaskLibService.class);
    //     CqTaskLibEntity cqTaskLibEntity = new CqTaskLibEntity();
    //     BeanUtil.copyProperties(cqTaskPlan, cqTaskLibEntity);
    //     return taskLibService.cultCountPrice(cqTaskLibEntity);
    // }
}
